package com.cdtye.itps.cms.controller.resource;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.config.CmsConfig;
import com.cdtye.itps.cms.dao.pillar.entity.JcwPillar;
import com.cdtye.itps.cms.service.pillar.JcwPillarService;
import com.cdtye.itps.cms.util.Constants;
import com.cdtye.itps.cms.util.StringUtils;
import com.cdtye.itps.cms.util.file.FileUploadUtils;
import com.cdtye.itps.cms.util.file.FileUtils;
import com.cdtye.itps.cms.util.file.MimeTypeUtils;
import com.cdtye.itps.cms.util.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 系统资源
 *
 * @author wzw
 */
@RestController
@RequestMapping("/resource")
public class SysResourceController {
    @Autowired
    private JcwPillarService jcwPillarService;

    @PostMapping(value = "/uploadZip")
    public AjaxJson<String> uploadZip(@RequestParam("file") MultipartFile file)  throws Exception{
        if (!file.isEmpty())
        {
            String imgPath = FileUploadUtils.upload(CmsConfig.getUploadPath(), file, MimeTypeUtils.PACKET_EXTENSION);
            // 本地资源路径
            String localPath = CmsConfig.getResource();
            // 数据库资源地址
            String filePath = localPath + StringUtils.substringAfter(imgPath, Constants.RESOURCE_PREFIX);
            FileUtils.unPackZip(filePath,null,StringUtils.substringBeforeLast(filePath, "/"));
            //todo 关联支柱
            String siteName= FileUtils.getNameNotSuffix(filePath);
            File[] fileList = FileUtils.getFileList(FileUtils.removeExtension(filePath));
            if(null != fileList && fileList.length>0){
                for (File img : fileList) {
                    if(img.isFile()){
                        String pillarNum = FileUtils.getNameNotSuffix(img.getAbsolutePath());
                        JcwPillar jcwPillar= jcwPillarService.getPillarBySiteNameAndPillarNum(siteName,pillarNum);
                        if(null != jcwPillar){
                            String uuid = UUID.randomUUID().toString(true);
                            String oldPath = img.getAbsolutePath();
                            String newPath = StringUtils.replace(img.getAbsolutePath(),pillarNum,uuid);
                            FileUtils.reNameFile(oldPath,newPath);
                            String pillarImgPath = CmsConfig.getPrefix()+FileUploadUtils.getPathFileName(newPath);
                            jcwPillarService.updatePillarImage(jcwPillar.getPillarId(),pillarImgPath);
                        }
                    }
                }
            }
            return AjaxJson.success();
        }
        return AjaxJson.error("上传图片异常，请联系管理员");
    }

    @PostMapping(value = "/uploadImage")
    public AjaxJson<String> uploadImage(@RequestParam("file") MultipartFile file)  throws Exception{
        if (!file.isEmpty())
        {
            String imgPath = FileUploadUtils.upload(CmsConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            imgPath = CmsConfig.getPrefix() + imgPath;
            return AjaxJson.success(imgPath);
        }
        return AjaxJson.error("上传图片异常，请联系管理员");
    }
}
